package com.sina.interfaces.web;

import com.sina.interfaces.core.util.Constant;
import com.sina.interfaces.web.config.ServiceConfig;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.support.SpringBootServletInitializer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Import;
import org.springframework.context.support.ResourceBundleMessageSource;
import org.springframework.web.servlet.view.BeanNameViewResolver;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * 段浩杰   2017-09-21-18:37
 */
@SpringBootApplication(exclude = HibernateJpaAutoConfiguration.class)
@Import({ServiceConfig.class/*, WebCustomConfig.class*/})
public class WebApplication extends SpringBootServletInitializer {

    public static void main(String[] args) {
        SpringApplication.run(WebApplication.class, args);
    }

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(WebApplication.class);
    }


    /*@Bean
    public SignatureJackson2HttpMessageConverter jsonConverter(SecretKeyManager secretKeyManager
            , @Qualifier("commonObjectMapper") ObjectMapper objectMapper) {
        SignatureJackson2HttpMessageConverter converter = new SignatureJackson2HttpMessageConverter( objectMapper );
        converter.setSecretKeyManager( secretKeyManager );
        List<MediaType> list = new ArrayList<>();
        list.add( MediaType.APPLICATION_JSON_UTF8 );
        list.add( MediaType.APPLICATION_JSON );
        converter.setSupportedMediaTypes( list );
        return converter;
    }*/

    /**
     * capture  controller exception
     */
    /*@Bean
    @Autowired
    public JsonViewExceptionResolver jsonViewExceptionResolver(
            @Qualifier("webLayerMessageSource") ResourceBundleMessageSource webLayerMessageSource
            ,@Qualifier("signature/result") View view) {
        JsonViewExceptionResolver jsonViewExceptionResolver = new JsonViewExceptionResolver();
        jsonViewExceptionResolver.setMessageSource( webLayerMessageSource );
        jsonViewExceptionResolver.setDefaultView( view );
        return jsonViewExceptionResolver;
    }*/

    /*json view Resolver start */
    @Bean
    public BeanNameViewResolver jsonViewResolver() {
        BeanNameViewResolver beanNameViewResolver = new BeanNameViewResolver();
        beanNameViewResolver.setOrder( 10 );
        return beanNameViewResolver;
    }

    @Bean
    public ResourceBundleMessageSource webLayerMessageSource(
            @Qualifier("serviceLayerMessageSource")
                    ResourceBundleMessageSource serviceLayerMessageSource) {
        ResourceBundleMessageSource resourceBundleMessageSource = new ResourceBundleMessageSource();
        resourceBundleMessageSource.setDefaultEncoding( Constant.UTF8 );
        resourceBundleMessageSource.setBasename( "i18n/web/messages" );
        resourceBundleMessageSource.setUseCodeAsDefaultMessage( true );
        resourceBundleMessageSource.setParentMessageSource( serviceLayerMessageSource );
        return resourceBundleMessageSource;
    }
}
